/*Table C-1*/

use "Raw Data/journalist_survey.dta", clear

merge m:1 state_name pc_num using "Analysis Data/PC_est_sample.dta", keep(matched) nogen
merge m:1 state_name pc_num using "Raw Data/ECI_sched_clean.dta", keep(matched) keepusing(poll_date) nogen

drop if secret_1==""

bys state_name pc_num: egen resptotal = count(poll_date)
tab resptotal

keep jcode state_name pc_num secret_1-secret_3 resptotal
reshape long secret_, i(state_name pc_num jcode) j(srno) string

drop if secret_ == ""
drop srno

tempfile num_pcs
preserve
bys resptotal state_name pc_num: drop if _n>1
replace resptotal= 6 if resptotal>6
gen x = 1
collapse (sum) num_pcs = x, by(resptotal)
save `num_pcs'
restore 

/*Calculating number of parties*/
tempfile num_parties
preserve
use "Raw Data/results_10states2014_clean.dta", clear
drop if ac_num<0
drop if cand_party == "IND"

replace cand_party = "NDA" if cand_party == "BJP"
replace cand_party = "NDA" if cand_party == "TDP" & state_name=="Andhra Pradesh"
replace cand_party = "NDA" if cand_party == "SHS" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "RSPS" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "RPI(A)" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "SWP" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "LJP" & state_name=="Bihar"
replace cand_party = "NDA" if cand_party == "BLSP" & state_name=="Bihar"
replace cand_party = "NDA" if cand_party == "AD" & state_name=="Uttar Pradesh"

replace cand_party = "UPA" if inlist(cand_party,"INC","RJD","BVA","MD")
replace cand_party = "UPA" if cand_party == "JMM" & state_name == "Jharkhand"
replace cand_party = "UPA" if cand_party == "NCP" & state_name == "Maharashtra"
replace cand_party = "UPA" if cand_party == "RLD" & state_name == "Uttar Pradesh"

bys state_name pc_num cand_party: drop if _n>1
bys state_name pc_num: egen num_parties = count(eci_srno)
keep state_name pc_num num_parties
bys state_name pc_num: drop if _n>1
save `num_parties'
restore

merge m:1 pc_num state_name using `num_parties', keep(matched) nogen

tempfile cand_parties
preserve
use "Raw Data/results_10states2014_clean.dta", clear
drop if ac_num<0
drop if cand_party == "IND"
replace cand_party = "NDA" if cand_party == "BJP"
replace cand_party = "NDA" if cand_party == "TDP" & state_name=="Andhra Pradesh"
replace cand_party = "NDA" if cand_party == "SHS" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "RSPS" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "RPI(A)" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "SWP" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "LJP" & state_name=="Bihar"
replace cand_party = "NDA" if cand_party == "BLSP" & state_name=="Bihar"
replace cand_party = "NDA" if cand_party == "AD" & state_name=="Uttar Pradesh"

replace cand_party = "UPA" if inlist(cand_party,"INC","RJD","BVA","MD")
replace cand_party = "UPA" if cand_party == "JMM" & state_name == "Jharkhand"
replace cand_party = "UPA" if cand_party == "NCP" & state_name == "Maharashtra"
replace cand_party = "UPA" if cand_party == "RLD" & state_name == "Uttar Pradesh"

keep state_name pc_num cand_party
bys state_name pc_num cand_party: drop if _n>1
save `cand_parties'
restore

rename secret_ cand_party

merge m:m pc_num state_name cand_party using `cand_parties', nogen
merge m:1 state_name pc_num using "Analysis Data/PC_est_sample.dta", keep(matched) nogen

bys state_name pc_num: egen x = min(resptotal)
replace resptotal = x 
drop x

bys state_name pc_num: egen x = min(num_parties)
replace num_parties = x 
drop x

gen vb = (jcode != "")
drop jcode
drop if resptotal==.

collapse (sum) vb, by(state_name pc_num resptotal num_parties cand_party)

replace resptotal = 6 if resptotal>6
replace vb = 6 if vb>6

tab vb, gen(vb_)
replace vb = (vb>0 & vb!=.)

collapse (mean) vb_? (sum) vb, by(state_name pc_num num_parties resptotal)
collapse (mean) vb_? (mean) num_parties (mean) vb, by(resptotal)

merge 1:1 resptotal using `num_pcs', nogen

order num_pcs resptotal vb_? num_parties vb
format vb_? %9.2f
format num_parties vb %9.1f
list


